/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.muni.fi.pb138.webrep;

import cz.muni.fi.pb138.webrep.metadata.processors.WarMetadataProcessor;
import cz.muni.fi.pb138.webrep.metadata.processors.WsdlMetadataProcessor;
import cz.muni.fi.pb138.webrep.metadata.processors.XsdMetadataProcessor;
import cz.muni.fi.pb138.webrep.util.Pair;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Odin
 */
@WebServlet(name = "FindByMetadataServlet", urlPatterns = {"/find"})
public class FindByMetadataServlet extends HttpServlet {
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.getRequestDispatcher(request.getContextPath()+"/view/findByMetadata.jsp")
                .forward(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int numOfRestrictions = Integer.parseInt(request.getParameter("numOfRestrictions"));
        
        List<Pair<String, String>> restrictions = new ArrayList<>();
        for(int i = 0; i < numOfRestrictions; i++) {
            if(!request.getParameter("restriction"+ i +"_value").equals("")) {
                restrictions.add(Pair.of(
                        request.getParameter("restriction"+ i +"_type"), 
                        request.getParameter("restriction"+ i +"_value")
                    ));
            }
        }
        
        Map<String, Set<Integer>> found = new HashMap<>();
        switch(request.getParameter("format")) {
            case "war":
                found = new WarMetadataProcessor().getArtifactsByMetadata(restrictions);
                break;
            case "wsdl":
                found = new WsdlMetadataProcessor().getArtifactVersionsByMetadata(restrictions);
                break;
            case "xsd": 
                found = new XsdMetadataProcessor().getArtifactVersionsByMetadata(restrictions);
                break;
        }
        
        request.setAttribute("found", found);
        
        request.getRequestDispatcher(request.getContextPath()+"/view/displayFound.jsp")
                .forward(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
